import { tokenToString } from 'typescript'
import {createRouter,createWebHistory,createWebHashHistory} from 'vue-router'
import { userToken } from "@/store/useToken";



/**
 * vue-router    3.x-    vue 2.x
 * vue-router    4.x  -- vue 3.x
 */
const router = createRouter(
    
    {
	// history:createWebHistory(), //有的需要后端特殊处理
	history:createWebHashHistory(), // 不会修改路径
	routes:[
		
		{
			path:'/home',
			redirect:'/'
		},
		{
			path:'/',
			component:()=> import('@/views/Home.vue'),
			children:[
				{
					path:'/department',
					component:()=> import('@/views/department/index.vue')
				},
				{
					path:'/drug',
					component:()=> import('@/views/drug/index.vue')
				},
				{
					path:'/uomg',
					component:()=> import('@/views/uomg/index.vue')
				},
				{
					path:'/pinia',
					component:()=> import('@/views/pinia/index.vue')
				},
				{
					path:'/pinia-setup',
					component:()=> import('@/views/pinia/Setup.vue')
				}
			
			]
		},
		{
			path:'/login',
			component:()=> import('@/views/Login.vue')
		},
		
	]
}


)

const whiteList = ['/login','/404','/error']

router.beforeEach((to,from,next)=>{

	
	console.log(
	  '%c from: ',
	  'background-color: #3756d4; padding: 4px 8px; border-radius: 2px; font-size: 14px; color: #fff; font-weight: 700;',
	  from 
	)


	
	console.log(
	  '%c to: ',
	  'background-color: #3756d4; padding: 4px 8px; border-radius: 2px; font-size: 14px; color: #fff; font-weight: 700;',
	  to 
	)
	
	const tokenStore =  userToken()

	
	if(whiteList.indexOf(to.path) != -1){ //白名单s
		next()
	}else{
		//获取token
		if(tokenStore.token){
			next()
		}else{
			next('/login')
		}

	}


})

export default router